const path = require('path');
const hx = require("hbuilderx");
const fs = require('fs')
const MessageHandler = require('./utils/messageHandler.js')

var uwebview = null
async function createWebView(){
	let webviewPanel = await hx.window.createWebView("extension.ucoderWebView",{
	        enableScripts:true
	    });
	console.log('加载网页')
	uwebview = webviewPanel.webView;
	uwebview.html =`
	<!doctype html>
	<html lang="en">
		<head>
			<meta charset="utf-8">
			<meta http-equiv="X-UA-Compatible" content="IE=edge">
			<meta name="viewport" content="width=device-width,initial-scale=1">
			<title>default</title>
			<script defer="defer" src="${path.join(__dirname, 'webview/js/chunk-vendors.js')}"></script>
			<script defer="defer" src="${path.join(__dirname, 'webview/js/app.js')}"></script>
			<link href="${path.join(__dirname, 'webview/static/css/chunk-vendors.css')}" rel="stylesheet">
			<link href="${path.join(__dirname, 'webview/static/css/app.css')}" rel="stylesheet">
		</head>
		<body><noscript><strong>We're sorry but default doesn't work properly without JavaScript enabled. Please enable it
					to continue.</strong></noscript>
			<div id="app"></div>
			<script>
				setTimeout(() => {
				  if (window.AIApp) {
					AIApp()
				  } else {
					console.log('初始化函数未找到')
				  }
				}, 100)
			  </script>
		</body>
	</html>

	`

// 	uwebview.html = `<!doctype html>
// <html lang="en">
// 	<head>
// 		<meta charset="utf-8">
// 		<meta http-equiv="X-UA-Compatible" content="IE=edge">
// 		<meta name="viewport" content="width=device-width,initial-scale=1">
// 		<title>ucoder</title>
// 	</head>
// 	<body style="margin:0;"><noscript><strong>We're sorry but default doesn't work properly without JavaScript enabled. Please enable it
// 				to continue.</strong></noscript>
// 		<iframe src="http://localhost:8081/#/" frameborder="0" style="width: 100%;height:100vh;"></iframe>
// 	</body>
// </html>`

	MessageHandler.getInstance(uwebview)
	
	hx.window.showView({
	  viewId: 'extension.ucoderWebView',
	});
	console.log('开启侧边窗口')
}


module.exports = {
	createWebView
}

